package br.com.fatecpg.operon.frames;

import br.com.fatecpg.operon.Conexao;
import com.sun.jndi.ldap.Connection;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;
        
        

public class CadastroCursoslFrame extends javax.swing.JInternalFrame {
            ResultSet CursoResultSet=null;
            
            

       public CadastroCursoslFrame() {
        initComponents();
        
         try{
        atualizar();

    }catch (SQLException ex){
        JOptionPane.showMessageDialog(this, "Erro: "+ex.getMessage(),
                "Erro",JOptionPane.ERROR_MESSAGE );
    
    }
    }
       
    public void atualizar () throws SQLException{
        DefaultTableModel model = (DefaultTableModel) Table_Pesquisa.getModel();
        model.setRowCount(0);
        String sql="SELECT * FROM operon.CURSOS";
        ResultSet rs=Conexao.con.createStatement().executeQuery(sql);
        while(rs.next()){
            model.addRow(new Object[]{
            rs.getInt("CURSO_ID"),
            rs.getString("SIGLA"),
            rs.getString("TITULO"),
            rs.getString("PERIODOS"),
            rs.getString("PERFIL_PROFISSIONAL")
        });
    }}

    
    public void pesquisar () throws SQLException{
        DefaultTableModel model = (DefaultTableModel) Table_Pesquisa.getModel();
        model.setRowCount(0);
        String sql="SELECT * FROM operon.CURSOS"+"WHERE SIGLA LIKE '%"+ pesquisaTextField.getText()+"%'";
        ResultSet rs=Conexao.con.createStatement().executeQuery(sql);
        while(rs.next()){
            model.addRow(new Object[]{
            rs.getInt("CURSO_ID"),
            rs.getString("SIGLA"),
            rs.getString("TITULO"),
            rs.getString("PERIODOS"),
            rs.getString("PERFIL_PROFISSIONAL")
        });
    }}
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idLabel = new javax.swing.JLabel();
        siglaLabel = new javax.swing.JLabel();
        tituloLabel = new javax.swing.JLabel();
        periodoLabel = new javax.swing.JLabel();
        perfilprofissionalLabel = new javax.swing.JLabel();
        idTextField = new javax.swing.JTextField();
        siglaTextField = new javax.swing.JTextField();
        tituloTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        perfilprofissonalTextArea = new javax.swing.JTextArea();
        periodoComboBox = new javax.swing.JComboBox();
        desativarButton = new javax.swing.JButton();
        cadastrarButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        Table_Pesquisa = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        L_Pesquisa = new javax.swing.JLabel();
        pesquisarButton = new javax.swing.JButton();
        pesquisaTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        cancelarButton = new javax.swing.JButton();
        atualizarButton = new javax.swing.JButton();
        alterarButton = new javax.swing.JButton();
        inserirButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Cadastro de Cursos");
        getContentPane().setLayout(null);

        idLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        idLabel.setText("ID:");
        getContentPane().add(idLabel);
        idLabel.setBounds(90, 150, 14, 20);

        siglaLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        siglaLabel.setText("Sigla:");
        getContentPane().add(siglaLabel);
        siglaLabel.setBounds(200, 160, 31, 20);

        tituloLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        tituloLabel.setText("Título:");
        getContentPane().add(tituloLabel);
        tituloLabel.setBounds(90, 190, 34, 15);

        periodoLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        periodoLabel.setText("Período:");
        getContentPane().add(periodoLabel);
        periodoLabel.setBounds(90, 220, 47, 15);

        perfilprofissionalLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        perfilprofissionalLabel.setText("Perfil Profissional:");
        getContentPane().add(perfilprofissionalLabel);
        perfilprofissionalLabel.setBounds(90, 250, 104, 15);

        idTextField.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        getContentPane().add(idTextField);
        idTextField.setBounds(130, 150, 49, 21);

        siglaTextField.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        getContentPane().add(siglaTextField);
        siglaTextField.setBounds(240, 150, 75, 21);

        tituloTextField.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        getContentPane().add(tituloTextField);
        tituloTextField.setBounds(130, 190, 198, 21);

        perfilprofissonalTextArea.setColumns(20);
        perfilprofissonalTextArea.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        perfilprofissonalTextArea.setRows(5);
        jScrollPane1.setViewportView(perfilprofissonalTextArea);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(90, 270, 330, 80);

        periodoComboBox.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        periodoComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "vespertino", "noturno" }));
        getContentPane().add(periodoComboBox);
        periodoComboBox.setBounds(150, 220, 100, 21);

        desativarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/icons/48px-Crystal_Clear_action_edit_remove.png"))); // NOI18N
        desativarButton.setToolTipText("Desativar");
        desativarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                desativarButtonActionPerformed(evt);
            }
        });
        getContentPane().add(desativarButton);
        desativarButton.setBounds(460, 290, 60, 50);

        cadastrarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/icons/48px-Crystal_Clear_action_edit_add.png"))); // NOI18N
        cadastrarButton.setToolTipText("Cadastrar");
        cadastrarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrarButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cadastrarButton);
        cadastrarButton.setBounds(460, 190, 60, 50);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(30, 120, 420, 10);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Cadastro de Cursos");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(110, 10, 240, 50);

        Table_Pesquisa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Sigla", "Título", "Período", "Perfil Profissional"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(Table_Pesquisa);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(20, 410, 560, 110);

        jPanel2.setBackground(new java.awt.Color(204, 204, 0));

        L_Pesquisa.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        L_Pesquisa.setText("Digite a sigla do curso:");

        pesquisarButton.setText("Pesquisar");
        pesquisarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarButtonActionPerformed(evt);
            }
        });

        pesquisaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisaTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(L_Pesquisa)
                .addGap(18, 18, 18)
                .addComponent(pesquisaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pesquisarButton)
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_Pesquisa)
                    .addComponent(pesquisarButton)
                    .addComponent(pesquisaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel2);
        jPanel2.setBounds(30, 70, 420, 40);

        jPanel3.setBackground(new java.awt.Color(204, 204, 0));

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(306, Short.MAX_VALUE)
                .addComponent(cancelarButton)
                .addGap(39, 39, 39))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(0, 217, Short.MAX_VALUE)
                .addComponent(cancelarButton))
        );

        getContentPane().add(jPanel3);
        jPanel3.setBounds(30, 140, 420, 240);

        atualizarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/icons/48px-Crystal_Clear_action_reload.png"))); // NOI18N
        atualizarButton.setToolTipText("Atualizar");
        atualizarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizarButtonActionPerformed(evt);
            }
        });
        getContentPane().add(atualizarButton);
        atualizarButton.setBounds(460, 340, 60, 50);

        alterarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/icons/48px-Crystal_Clear_app_kwrite.png"))); // NOI18N
        alterarButton.setToolTipText("Alterar");
        alterarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarButtonActionPerformed(evt);
            }
        });
        getContentPane().add(alterarButton);
        alterarButton.setBounds(460, 240, 60, 50);

        inserirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/icons/Crystal_Clear_mimetype_mime_txt.png"))); // NOI18N
        inserirButton.setToolTipText("Inserir");
        inserirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inserirButtonActionPerformed(evt);
            }
        });
        getContentPane().add(inserirButton);
        inserirButton.setBounds(460, 130, 60, 60);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/fatecpg/operon/imagens/escola_1024x768.jpg"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, 0, 620, 540);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-636)/2, (screenSize.height-566)/2, 636, 566);
    }// </editor-fold>//GEN-END:initComponents

    private void desativarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_desativarButtonActionPerformed
        if (Table_Pesquisa.getSelectedRow()>=0){
            String código = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),0).toString();
        if(JOptionPane.showConfirmDialog(this, "Confirma exclusão do registro"+código)==JOptionPane.YES_OPTION){
            String SQL="DELETE FROM operon.CURSOS WHERE CURSO_ID="+código;
        try{
            Conexao.con.createStatement().execute(SQL);
            this.atualizar();
        }catch (SQLException ex){
            JOptionPane.showMessageDialog(this, "0 registro não pode ser excluido. Erro: "+ex.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
        }}
        }
    }//GEN-LAST:event_desativarButtonActionPerformed

    private void cadastrarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarButtonActionPerformed
        String id= idTextField.getText();
        String sigla= siglaTextField.getText();
        String titulo= tituloTextField.getText();
        String periodo = (String) periodoComboBox.getSelectedItem();
        String perfil = perfilprofissonalTextArea.getText();
        String subQuery = "SELECT CURSO_ID FROM operon.CURSOS WHERE PERIODOS = '" + periodo + "'";
        String SQL = "INSERT INTO operon.CURSOS VALUES ('" + id + "', '" + sigla + "', '" + titulo + "', (" + subQuery + "), '"+ perfil +"')";
            try{
                Conexao.con.createStatement().execute(SQL);
                this.dispose();
                
            }catch (SQLException ex){
                JOptionPane.showMessageDialog(this, "O registro não pode ser inserido. Erro:" + ex.getMessage(),"Erro", JOptionPane.ERROR_MESSAGE);                                            



    }
    }//GEN-LAST:event_cadastrarButtonActionPerformed

    private void pesquisarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarButtonActionPerformed
        try {
            pesquisar();

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Erro: " + ex.getMessage(),
                    "Erro", JOptionPane.ERROR_MESSAGE);
            this.dispose();
        }

    }//GEN-LAST:event_pesquisarButtonActionPerformed

    private void pesquisaTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisaTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pesquisaTextFieldActionPerformed

    private void atualizarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizarButtonActionPerformed
        try{
            atualizar();
        }catch (SQLException ex){
            JOptionPane.showMessageDialog(this, "Erro: "+ex.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_atualizarButtonActionPerformed

    private void alterarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarButtonActionPerformed
        if (Table_Pesquisa.getSelectedRow()>=0){
            String código = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),0).toString();
            String sigla = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),1).toString();
            String titulo = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),2).toString();
            String periodo = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),3).toString();
            String perfil = Table_Pesquisa.getValueAt(Table_Pesquisa.getSelectedRow(),4).toString();
        if(JOptionPane.showConfirmDialog(this, "Confirma alteração do registro"+código)==JOptionPane.YES_OPTION){
            String SQL="UPDATE operon.CURSOS" + "SET SIGLA = '" + sigla + "', '" + titulo + "', '"+ periodo +"', '"+ perfil +"'" + "WHERE CURSO_ID =" + código;
                    try{
            Conexao.con.createStatement().execute(SQL);
            this.atualizar();
        }catch (SQLException ex){
            JOptionPane.showMessageDialog(this, "0 registro não pode ser alterado. Erro: "+ex.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
        }}
        }
    }//GEN-LAST:event_alterarButtonActionPerformed

    private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
        idTextField.setEditable(false);
        siglaTextField.setEditable(false);
        tituloTextField.setEditable(false);
        periodoComboBox.setEditable(false);
        perfilprofissonalTextArea.setEditable(false);
        cancelarButton.setEnabled(false);
        idTextField.setText("");
        siglaTextField.setText("");
        tituloTextField.setText("");
        periodoComboBox.setSelectedItem("");
        perfilprofissonalTextArea.setText("");
    }//GEN-LAST:event_cancelarButtonActionPerformed

    private void inserirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inserirButtonActionPerformed
        idTextField.setEditable(true);
        siglaTextField.setEditable(true);
        tituloTextField.setEditable(true);
        periodoComboBox.setEditable(true);
        perfilprofissonalTextArea.setEditable(true);
        cancelarButton.setEnabled(true);
    }//GEN-LAST:event_inserirButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel L_Pesquisa;
    private javax.swing.JTable Table_Pesquisa;
    private javax.swing.JButton alterarButton;
    private javax.swing.JButton atualizarButton;
    private javax.swing.JButton cadastrarButton;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JButton desativarButton;
    private javax.swing.JLabel idLabel;
    private javax.swing.JTextField idTextField;
    private javax.swing.JButton inserirButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel perfilprofissionalLabel;
    private javax.swing.JTextArea perfilprofissonalTextArea;
    private javax.swing.JComboBox periodoComboBox;
    private javax.swing.JLabel periodoLabel;
    private javax.swing.JTextField pesquisaTextField;
    private javax.swing.JButton pesquisarButton;
    private javax.swing.JLabel siglaLabel;
    private javax.swing.JTextField siglaTextField;
    private javax.swing.JLabel tituloLabel;
    private javax.swing.JTextField tituloTextField;
    // End of variables declaration//GEN-END:variables
}
